<template>
	<view class="content openWz-container">
		<view class="title">提交身份认证信息</view>
		<view class="tip f-24">使用系统前需开通居民身份网络可信凭证（CTID）</view>
		
		<view class="input-box">
			<view class="uni-form-item uni-column">
				<view class="label flexbox flexbox-middle">姓名：</view>
				<input class="uni-input" :disabled="!isEdit" v-model="formInfo.name" @input="onKeyInput" placeholder="请输入姓名" />
			</view>
			
			<view class="uni-form-item uni-column">
				<view class="label flexbox flexbox-middle">身份证：</view>
				<input class="uni-input" :disabled="!isEdit"  type="idcard" @input="onKeyInput" v-model="formInfo.cardNum" placeholder="请输入身份证" />
			</view>
		</view>
		
		
		<view class="open-btn flexbox flexbox-middle flexbox-center f-32" :class="{'disabled':!activeFlag}" @click="submitInfo">下一步</view>
		<login-alert :isShow="loginShow" @loginSuccess="loginSuccess" @closeLogin="closeLogin"/>
	</view>

</template>

<script>
	import {isIdCard, isName} from '../../static/js/utils.js'
	import loginAlert from '@/components/loginAlert/index.vue'
	export default {
		components:{
			loginAlert
		},
		data() {
			return {
				formInfo: {
					name: '',
					cardNum: '',
					idcardAuthData: '',
					bsn:''
				},
				activeFlag: false,
				type: '',
				id: '',
				scan_flag: '',
				rurl: '',
				isEdit: true,
				query: {},
				loginShow: false
			}
		},
		onLoad(e) {
			this.query = e
			this.type = e.type
			this.id = e.id || ''
			this.rurl = e.rurl || ''
			this.scan_flag = e.scan_flag || ''
			if (uni.getStorageSync('authorization')) {
				if(uni.getStorageSync('role') != 'user') {
					this.$HTTP.post('/audit/getIdentity', {}, false).then((res) => {
						this.formInfo.name = res.data.name
						this.formInfo.cardNum = res.data.idNo
						if(res.data.name) {
							this.isEdit = false
						}
						this.activeFlag = true
					}).catch(err => {
						
					})
				}
			}
		},
		onShow: function() {
			if (uni.getStorageSync('authorization')) {
				this.loginShow = false
				this.getData()
			} else {
				this.loginShow = true
			}
		},
		methods: {
			submitInfo() {
				if (!isName(this.formInfo.name)) {
					uni.showToast({
					    title: '请填写正确格式姓名',
					    icon: 'none'
					})
					return false
				}
				if (!isIdCard(this.formInfo.cardNum)) {
					uni.showToast({
					    title: '请填写正确格式身份证',
					    icon: 'none'
					})
					return false
				}
				
				if(!uni.getStorageSync('authorization')){
					uni.redirectTo({
						url: `/pages/login/index?rurl=${encodeURIComponent('/pages/openWz-step1/index')}&type=companyAuth&noAuth=1`
					});
					return false
				}
				let cer_id = uni.getStorageSync('cer_id') || '';
				if (this.type) {
					uni.redirectTo({
					    url: `/pages/openWz-step2/index?rurl=${this.rurl}&type=${this.type}&cardNum=${this.formInfo.cardNum}&name=${this.formInfo.name}&idcardAuthData=${encodeURIComponent(this.formInfo.idcardAuthData)}&bsn=${encodeURIComponent(this.formInfo.bsn)}&cer_id=${cer_id}&id=${this.id}&scan_flag=${this.scan_flag}&phone=${this.query.phone}`
					});
				} else {
					uni.navigateTo({
					    url: `/pages/openWz-step2/index?rurl=${this.rurl}&type=${this.type}&cardNum=${this.formInfo.cardNum}&name=${this.formInfo.name}&idcardAuthData=${encodeURIComponent(this.formInfo.idcardAuthData)}&bsn=${encodeURIComponent(this.formInfo.bsn)}&cer_id=${cer_id}&id=${this.id}&scan_flag=${this.scan_flag}&phone=${this.query.phone}`
					});
				}
				
			},
			getData() {
				var IdCardData =  new this.$WZAPP.EnrollmentIDCardData('0013', '00001105');
				this.$HTTP.post('/ctid/unity', {type: this.$BSNCODE.AUTHENTICATION_DOWNLOAD}, false).then((res) => {
					var varIdCard = this.$WZAPP.getEnrollmentIDCardData (res.data.randomNumber, IdCardData)
					this.formInfo.idcardAuthData = varIdCard.value
					this.formInfo.bsn = res.data.bsn
					uni.setStorageSync('firstBsn', res.data.bsn);
				}).catch(err => {
					
				})
			},
			loginSuccess() {
				this.loginShow = false
				this.getData()
			},
			closeLogin() {
				this.loginShow = false
			},
			onKeyInput() {
				if (isName(this.formInfo.name) && isIdCard(this.formInfo.cardNum)) {
					this.activeFlag = true
				} else {
					this.activeFlag = false
				}
			}
		}


	}
</script>

<style lang="scss" scoped>
	@import "./index.scss";
</style>